package servlet4;

import com.google.gson.Gson;
import entity.UserLogin;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;



//好友列表
@WebServlet("/friend")
public class friendServlet extends HttpServlet {

    private String encode;
    private String contentType;

    private UserService userSer = new UserService();
    UserLogin user =new UserLogin();
    @Override
    public void init() throws ServletException {
        super.init();
        encode = this.getServletContext().getInitParameter("encode");
        contentType = this.getServletContext().getInitParameter("contentType");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding(encode);
        resp.setCharacterEncoding(encode);
        Cookie[] cookies = req.getCookies();

        String newToken = null;
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("TOKEN")) {
                newToken = cookie.getValue();
            }
        }
        List friendCuont = userSer.getfriendCuont(newToken);
//        resp.getWriter().print(newToken);
        resp.getWriter().print(new Gson().toJson(friendCuont));

    }
}